package com.reach.redis.interceptor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.reach.redis.bo.CacheConfig;

public abstract class AbstractCacheInterceptor {
	
	protected String getClassName(String name) {
		return name.substring(0, name.indexOf("$$"));
	}
	
	protected String getFullKey(Object[] object,CacheConfig cacheConfig,String key) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		StringBuilder sb = new StringBuilder();
		sb.append(getModule(cacheConfig));
    	sb.append("_");
    	sb.append(getEvent(cacheConfig));
    	sb.append("_");
    	sb.append(key);
    	return String.valueOf(sb);
	}
	
	protected String getKey(Object[] object,CacheConfig cacheConfig) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    	StringBuilder sb = new StringBuilder();
    	if(cacheConfig == null) {
    		return String.valueOf(sb);
    	}
    	
    	
    	
    	for (int i = 0; i < object.length; i++) {
			Object obj = object[i];
			if(obj == null || "null".equals(obj)) {
				continue;
			}
			
			if(obj instanceof Map) {
				@SuppressWarnings("unchecked")
				Map<Object,Object> map =  (Map<Object,Object>) obj;
				for(String  k : getAnnotation(cacheConfig)) {		
					Object v = map.get(k);					
					sb.append(v);
					sb.append("_");
					
				}
				continue;
				
			}
		
			for(String  k : getAnnotation(cacheConfig)) {
				if(isExists(obj, k)) {
					Method method = obj.getClass().getMethod("get"+upper(k));				
					if(method == null) {
						continue;
					}
					
					String v = String.valueOf(method.invoke(obj));
					sb.append(v);
					sb.append("_");
				}else{
					sb.append(obj);
					sb.append("_");
					break;
				}
			}
		}
    	
    	if(sb.toString().endsWith("_")) {
    		sb.delete(sb.length()-1, sb.length());
    	}
    	
    	return String.valueOf(sb);
    }
	
	
	protected Object getValue(Object[] object,CacheConfig cacheConfig,String key){
    	StringBuilder sb = new StringBuilder();
    	if(cacheConfig == null) {
    		return String.valueOf(sb);
    	}
    	try {
	    	for (int i = 0; i < object.length; i++) {
				Object obj = object[i];
				if(obj == null || "null".equals(obj)) {
					continue;
				}
				
				if(obj instanceof Map) {
					@SuppressWarnings("unchecked")
					Map<Object,Object> map =  (Map<Object,Object>) obj;
					Object v = map.get(key);
					if(v != null && !"".equals(v)) {
						return v;
					}
				}
			
				for(String  k : getAnnotation(cacheConfig)) {
					if(isExists(obj, k)) {
						Method method = obj.getClass().getMethod("get"+upper(k));				
						if(method == null) {
							continue;
						}
						
						Object v = method.invoke(obj);
						if(v != null&& !"".equals(v)) {
							return v;
						}
					}
				}
			}
    	}catch(Exception e) {
    		
    	}
    	
    	if(sb.toString().endsWith("_")) {
    		sb.delete(sb.length()-1, sb.length());
    	}
    	
    	return String.valueOf(sb);
    }
	
	private boolean isExists(Object obj,String field) {
		Method[]  objArray = obj.getClass().getMethods();
		for(Method v : objArray) {
			if(v.getName().toLowerCase().endsWith(field.toLowerCase())) {
				return true;
			}
		}
		return false;
	}
	
	public static String upper(String str) {
		return str.substring(0,1).toUpperCase() + str.substring(1);
	}
	
	public abstract String getModule(CacheConfig cacheConfig) ;
	
	public abstract String getEvent(CacheConfig cacheConfig);
	
	public abstract String[] getAnnotation(CacheConfig cacheConfig);
		
	public  Integer getExpire(Object[] object,CacheConfig cacheConfig) {
		return -1;
	}


	public String[] getTrim(String[] str) {
		if(str == null || str.length <=0) {
			return str;
		}
		String[] temp = new String[str.length];
		for(int i=0;i<str.length;i++) {
			temp[i] = str[i].trim(); 
		}
		return temp;
	}

}
